<template>
	<view class="">
		<!-- commontext -->
		<view class="main_title">
			企业信息
		</view>
		<view class="form_box">
			<view class="input_block flex justify align">
				<view class="input_text">
					<input type="text" v-model="enterpriseName" value="" placeholder="填写企业名称"/>
				</view>
			</view>
		</view>
		<view class="main_title">
			上传营业执照副本
		</view>
		<view class="form_box">
			<view class="input_block flex wrap align">
				<view class="image_box" v-for="(item,index) in imgList1" :key="index" @tap="previewImg(item)">
					<image :src="item" mode="aspectFill"></image>
					<view class="del_icon" @tap="del(1,index)">
						<image src="../../static/icon/del2.png" mode="aspectFill"></image>
					</view>
				</view>
				<view class="add_img" @tap="uploadImg(1)">
					<image src="../../static/icon/add2.png" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="" v-if="user.group_id == 3">
			<view class="main_title">
				上传建筑业企业资质证书
			</view>
			<view class="form_box">
				<view class="input_block flex wrap align">
					<view class="image_box" v-for="(item,index) in imgList2" :key="index" @tap="previewImg(item)">
						<image :src="item" mode="aspectFill"></image>
						<view class="del_icon" @tap="del(2,index)">
							<image src="../../static/icon/del2.png" mode="aspectFill"></image>
						</view>
					</view>
					<view class="add_img" @tap="uploadImg(2)">
						<image src="../../static/icon/add2.png" mode="aspectFill"></image>
					</view>
				</view>
			</view>
			<view class="main_title">
				上传安全生产许可证
			</view>
			<view class="form_box">
				<view class="input_block flex wrap align">
					<view class="image_box" v-for="(item,index) in imgList3" :key="index" @tap="previewImg(item)">
						<image :src="item" mode="aspectFill"></image>
						<view class="del_icon" @tap="del(3,index)">
							<image src="../../static/icon/del2.png" mode="aspectFill"></image>
						</view>
					</view>
					
					<view class="add_img" @tap="uploadImg(3)">
						<image src="../../static/icon/add2.png" mode="aspectFill"></image>
					</view>
				</view>
			</view>
			<view class="main_title">
				上传获奖及受表彰情况
			</view>
			<view class="form_box">
				<view class="input_block flex wrap align">
					<view class="image_box" v-for="(item,index) in imgList4" :key="index" @tap="previewImg(item)">
						<image :src="item" mode="aspectFill"></image>
						<view class="del_icon" @tap="del(4,index)">
							<image src="../../static/icon/del2.png" mode="aspectFill"></image>
						</view>
					</view>
					
					<view class="add_img" @tap="uploadImg(4)">
						<image src="../../static/icon/add2.png" mode="aspectFill"></image>
					</view>
				</view>
			</view>
		</view>
		
		<view class="btn" @tap="tosubmit">
			提交审核
		</view>
	</view>

</template>

<script>
	const app = getApp()
	export default {
		data() {
			return {
				enterpriseName:'',
				imgList1:[],
				imgList2:[],
				imgList3:[],
				imgList4:[],
				imgList5:[],//2项目方图片
				user:app.globalData.user
			}
		},
		methods: {
			// 图片上传
			uploadImg(ind){
				const that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function (res) {
						console.log(JSON.stringify(res.tempFilePaths));
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: app.globalData.IP+'api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							success: (uploadFileRes) => {
								let data = JSON.parse(uploadFileRes.data)
								console.log(that['imgList'+ind],uploadFileRes.data);
								that['imgList'+ind].push(data.data.fullurl)
							}
						});
					}
				});
			},
			// 预览图片
			previewImg(item){
				uni.previewImage({
					urls: [item],
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			// 删除图片
			del(ind,inedx){
				that['imgList'+ind].splice(index,1)
			},
			
			// 提交
			tosubmit(){
				let url = ''
				if(this.enterpriseName == ''){
					uni.showToast({
						title:'请填入企业名称',
						icon: 'none'
					})
					return 
				}
				if(this.imgList1.length <= 0){
					uni.showToast({
						title:'请上传营业执照副本',
						icon: 'none'
					})
					return 
				}
				if(this.user.group_id == 2){//项目方 一个图片
					url ='addons/shopro/verify_project/apply'
					let params = {
						name: this.enterpriseName,
						image: this.imgList1.join(',')
					}
					uni.showLoading({
						title:'上传中'
					})
					uni.request({
						url: app.globalData.IP + url,
						data: params,
						header: {
							token: app.globalData.token
						},
						method: 'POST',
						success: res => {
							console.log(res)
							if (res.data.code == 1) {
								console.log('-------->',res)
								uni.hideLoading()
								if(res.data.code == 1){
									uni.showToast({
										title: '上传成功'
									})
									setTimeout(function(){
										uni.switchTab({
											url:'/pages/my/my'
										})
									},1000)
								}else{
									uni.showToast({
										title: '上传失败',
										icon:'none'
									})
								}
							} else {
								uni.showToast({
									title: '上传失败',
									icon: 'none'
								})
							}
						},
						fail: err => {
							reject(err)
							console.log('发送数据失败')
						}
					})
				}
				if(this.user.group_id == 3){//接包方 四个图片
					url ='addons/shopro/verify_package/apply'
					if(this.imgList2.length <= 0){
						uni.showToast({
							title:'请上传建筑业企业资质证书',
							icon: 'none'
						})
						return 
					}
					if(this.imgList3.length <= 0){
						uni.showToast({
							title:'请上传安全生产许可证',
							icon: 'none'
						})
						return 
					}
					if(this.imgList4.length <= 0){
						uni.showToast({
							title:'请上传获奖及受表彰情况',
							icon: 'none'
						})
						return 
					}
					let params = {
						name: this.enterpriseName,
						image1: this.imgList1.join(','),
						image2: this.imgList2.join(','),
						image3: this.imgList3.join(','),
						image4: this.imgList4.join(',')
					}
					uni.showLoading({
						title:'上传中'
					})
					uni.request({
						url: app.globalData.IP + url,
						data: params,
						header: {
							token: app.globalData.token
						},
						method: 'POST',
						success: res => {
							console.log(res)
							if (res.data.code == 1) {
								console.log('-------->',res)
								uni.hideLoading()
								if(res.data.code == 1){
									uni.showToast({
										title: '上传成功'
									})
									setTimeout(function(){
										uni.switchTab({
											url:'/pages/my/my'
										})
									},1000)
								}else{
									uni.showToast({
										title: '上传失败',
										icon:'none'
									})
								}
							} else {
								uni.showToast({
									title: '上传失败',
									icon: 'none'
								})
							}
						},
						fail: err => {
							reject(err)
							console.log('发送数据失败')
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		padding: 40rpx 0;
	}
	.form_box{
		padding: 45rpx 30rpx;
		.flex_right{
			justify-content: flex-end;
		}
	}
	.input_block{
		margin-bottom: 30rpx;
	}
	.input_block:last-child{
		margin-bottom: 0;
	}
	.input_title{
		color: #000;
		
	}
	.main_title{
		font-size: 30rpx;
		color: #000;
		padding: 0 28rpx;
		font-weight: 600;
	}
	.image_box{
		width: 190rpx;
		height: 190rpx;
		position: relative;
		margin-right: 28rpx;
		margin-bottom: 28rpx;
		image{
			width: 190rpx;
			height: 190rpx;
			border-radius: 20rpx;
		}
		.del_icon{
			position: absolute;
			right: 0;
			top: 0;
			width: 34rpx;
			height: 34rpx;
			background-color: rgba(0, 0, 0, 0.4);
			border-radius: 50%;
			text-align: center;
			image{
				width: 24rpx;
				height: 24rpx;
				margin-top: 4rpx;
			}
		}
	}
	.add_img{
		width: 190rpx;
		height: 190rpx;
		border-radius: 20rpx;
		text-align: center;
		background-color: #f7f7f7;
		image{
			width:90rpx;
			height: 90rpx;
			margin-top: 50rpx;
		}
	}
	.btn{
		height: 110rpx;
		line-height: 110rpx;
		text-align: center;
		color: #fff;
		background: var(--base);
		border-radius: 100rpx;
		margin: 60rpx 60rpx 0;
	}
</style>
